
package com.idealighter.game.games.sirenniuniu;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.dictionary.domain.SirenniuniuRoomDomain;
import com.idealighter.game.games.sirenniuniu.message.ReqEnterGameHallMsg;
import com.idealighter.game.games.sirenniuniu.message.ReqEnterRoomMsg;
import com.idealighter.game.games.sirenniuniu.message.ReqFastEnterTableMsg;
import com.idealighter.game.robot.core.Player;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

/**
 * 四人牛牛机器人.
 *
 */
public class SiRenNiuNiuPlayer extends Player {

  public SirenniuniuRoomDomain roomDomain;

  /**
   * 构造函数.
   * 
   * @param playerId .
   * @param userName .
   * @param roomId .
   * @param initGold .
   * @param totalGameNum .
   * @param tableGameNum .
   */
  public SiRenNiuNiuPlayer(long playerId, String userName, int roomId, int initGold,
      int totalGameNum, int tableGameNum, RobotConfigDomain configDomain,
      SirenniuniuRoomDomain roomDomain) {
    super(playerId, userName, roomId, initGold, totalGameNum, tableGameNum, configDomain);
    this.roomDomain = roomDomain;
  }

  @Override
  public boolean goldCheck() {
    long gold = playerInfo.getGold();
    if (gold < roomDomain.getLower()) {
      LOG.info("[四人牛牛]机器人[{}][{}]金币小于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold > roomDomain.getUpper()) {
      LOG.info("[四人牛牛]机器人[{}][{}]金币高于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold < roomDomain.getAfee()) {
      LOG.info("[四人牛牛]机器人[{}][{}]金币小于台费退出游戏", userName, playerId);
      delayLogout();
      return false;
    }
    return true;
  }

  @Override
  public void enterGameHall() {
    sendMsg(new ReqEnterGameHallMsg());
  }

  @Override
  public void enterRoom() {
    if (rooms.contains(roomId)) {
      if (!goldCheck()) {
        return;
      }
      ReqEnterRoomMsg msg = new ReqEnterRoomMsg();
      msg.setRoomId(roomId);
      sendMsg(msg);
    }
  }

  @Override
  public void enterTable() {
    if (!goldCheck()) {
      return;
    }
    ReqFastEnterTableMsg msg = new ReqFastEnterTableMsg();
    msg.setRoomId(roomId);
    schedule(() -> sendMsg(msg), RandCodeUtil.random(3, 5), TimeUnit.SECONDS);
  }

  @Override
  public Game game() {
    return Game.SIREN_NIUNIU;
  }

  @Override
  public boolean enterPwdTableAble() {
    return false;
  }

}
